package com.jgsu.farm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jgsu.farm.entity.*;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.mapper.BiologyCategoryMapper;
import com.jgsu.farm.mapper.PestStressMapper;
import com.jgsu.farm.mapper.PlantMapper;
import com.jgsu.farm.mapper.StressCategoryMapper;
import com.jgsu.farm.mapper.sql.MatchImageMapperSql;
import com.jgsu.farm.mapper.sql.PestStressMapperSql;
import com.jgsu.farm.service.PestStressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class PestStressServiceImpl implements PestStressService {
    @Autowired
    PestStressMapper pestStressMapper;
    @Autowired
    PestStressMapperSql pestStressMapperSql;
    @Autowired
    StressCategoryMapper stressCategoryMapper;
    @Autowired
    BiologyCategoryMapper biologyCategoryMapper;
    @Autowired
    PlantMapper plantMapper;
    @Autowired
    MatchImageMapperSql matchImageMapperSql;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void add(PestStress pestStress) {
        StressCategory stressCategory = stressCategoryMapper.selectByPrimaryKey(pestStress.getStresstype());
        Plant plant = plantMapper.selectByPrimaryKey(pestStress.getCropid());
        BiologyCategory biologyCategory = biologyCategoryMapper.selectByPrimaryKey(pestStress.getCategory());
        List<MatchImage> pathoImage = matchImageMapperSql.selectByMatchId(pestStress.getPestimage());
        List<MatchImage> symptomImage = matchImageMapperSql.selectByMatchId(pestStress.getSyptomimage());
        if (stressCategory != null && plant != null && biologyCategory != null && !pestStress.getChinesename().isEmpty() &&
                !pathoImage.isEmpty() && !symptomImage.isEmpty()) {
            pestStressMapper.insert(pestStress);
        } else {
            throw new DataAddException("604", "不存在stressId或plantID或biologyCategoryId或pathoImage或symptomImage");
        }
    }

    @Override
    public void delete(Integer pestid) {
        int i = pestStressMapper.deleteByPrimaryKey(pestid);
        if (i == 0) {
            throw new DataNotExistException("603", "不存在该pestid，草害胁迫删除失败");
        }
    }

    @Override
    public PageInfo<PestStress> selectAll(int page, int pagesize) {
        PageHelper.startPage(page, pagesize);
        // 查询redis中是否存在
        List<PestStress> pestStresses = (List<PestStress>) redisTemplate.opsForValue().get("PestStresses");
        // 双重检测锁机制，解决缓存穿透问题，从而处理高并发要求
        if (null == pestStresses) {
            synchronized (this) {
                pestStresses = (List<PestStress>) redisTemplate.opsForValue().get("PestStresses");
                if (null == pestStresses) {
                    pestStresses = pestStressMapperSql.selectAll();
                    log.info("从数据库中查询。。。。");
                    redisTemplate.opsForValue().set("PestStresses", pestStresses);
                    log.info("将从数据库中查询的PestStresses数据存入redis。。。。");
                }
            }
        }
        PageInfo<PestStress> pageInfoPestStressList = new PageInfo<PestStress>(pestStresses);
        return pageInfoPestStressList;
    }

    @Override
    public void update(PestStress pestStress) {
        PestStress pestStressId = pestStressMapper.selectByPrimaryKey(pestStress.getPestid());
        if (pestStressId != null) {
            List<MatchImage> pathoImage = matchImageMapperSql.selectByMatchId(pestStress.getPestimage());
            List<MatchImage> symptomImage = matchImageMapperSql.selectByMatchId(pestStress.getSyptomimage());
            StressCategory stressCategory = stressCategoryMapper.selectByPrimaryKey(pestStress.getStresstype());
            Plant plant = plantMapper.selectByPrimaryKey(pestStress.getCropid());
            BiologyCategory biologyCategory = biologyCategoryMapper.selectByPrimaryKey(pestStress.getCategory());
            if (stressCategory != null && plant != null && biologyCategory != null && !pestStress.getChinesename().isEmpty() &&
                    !pathoImage.isEmpty() && !symptomImage.isEmpty()) {
                pestStressMapper.updateByPrimaryKey(pestStress);
            } else {
                throw new DataNotExistException("603", "不存在pestStressId或plantID或biologyCategoryId或pathoImage或symptomImage");
            }
        }else {
            throw new DataNotExistException("603", "不存在pestStressId");
        }
    }
}

